package inger.redpac.transcoders;

import org.bson.BSON;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;

public class BsonTranscoder implements Transcoder {

	@Override
	public byte[] pack(Object obj) throws Exception {
		if (obj instanceof BSONObject) {
			BasicBSONObject bsonObj = (BasicBSONObject) obj;
			return BSON.encode(bsonObj);
		} else {
			throw new Exception(
					"PLZ GIMME BSONObject!!! (I'm BsonTranscoder!!!)");
		}
	}

	@Override
	public Object unpack(byte[] bytes) throws Exception {
		BSONObject bsonObj = BSON.decode(bytes);
		return bsonObj;
	}

}
